﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.Model;
using Gift.DAL;

namespace WebApp.admin.message
{
    public partial class sendMessage : BaseAdmin
    {
        // Fiel
     
        tbOutBox oOutBox = new tbOutBox();

        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                this.oOutBox.sender = "管理员";
                this.oOutBox.addressee = this.txtAddressee.Value;
                this.oOutBox.title = this.txtTitle.Value;
                this.oOutBox.content = this.txtaContent.Value;
              
                this.oOutBox.CompanyId = int.Parse(base.Administrator.CompanyId);
                if (BaseFactory.theOutBox.SendEmail(this.oOutBox))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送成功！\");location.href=\"messageOutboxList.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送失败！\");</script>");
                }
            }
            catch
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送失败！\");</script>");
            }
        }

        public StringBuilder GetBcakLocation()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if (base.Request.QueryString["t"] == null)
                {
                    return builder;
                }
                if (base.Request.QueryString["t"] == "ib")
                {
                    builder.Append("location.href='messageInboxList.aspx';");
                    return builder;
                }
                builder.Append("location.href='messageOutboxList.aspx';");
            }
            catch
            {
            }
            return builder;
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if ((base.Request.QueryString["ir"] != null) && (base.Request.QueryString["id"] != null))
                {
                    builder.Append("回复消息");
                    return builder;
                }
                builder.Append("发送消息");
            }
            catch
            {
            }
            return builder;
        }

        private void OutBoxBind()
        {
            try
            {
                if (Convert.ToInt32(base.Request.QueryString["ir"]) == 0)
                {
                    BaseFactory.theInBox.ModifyInBoxReadById(Convert.ToInt32(base.Request.QueryString["id"]));
                }
                tbInBox inBoxById = BaseFactory.theInBox.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (inBoxById != null)
                {
                    this.txtAddressee.Value = inBoxById.sender;
                    this.txtAddressee.Attributes.Add("readonly", "readonly");
                    this.txtTitle.Value = inBoxById.title;
                    this.txtaContent.Value = inBoxById.content;
                }
            }
            catch
            {
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(DALUser));
            //if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            //{
            //    base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            //}
            if (!base.IsPostBack && ((base.Request.QueryString["ir"] != null) && (base.Request.QueryString["id"] != null)))
            {
                this.OutBoxBind();
            }
        }

    }
}